package lxh.groovy.basic;
import groovy.xml.MarkupBuilder
import groovy.xml.StreamingMarkupBuilder
import org.custommonkey.xmlunit.*

def out  =   new  StringWriter()
def xb  =   new  MarkupBuilder(out)

def adds  =  [ 'home' ,  'company' ]


//out << "<?xml version='1.0' encoding='GBK'>\n";
xb.aboutMe 
{
	myName(type:"realName","���λ�a")
	myName(type:"webName","��������a")
	birth("1979-01-04")
	addresses(num:adds.size()) 
	{
		for(a in adds) 
		{
			address(a)
		}
	}
}

println out.toString()

def friendList = ['Tony', 'Alan', 'Leona', 'Cloudy']
def xml = new StreamingMarkupBuilder().bind { 
    mkp.pi(xml: "version='1.0'  encoding='UTF-8'") 
    
    mkp.declareNamespace(ns:"http://www.ebay.com")
    person {
        ns.name(type: "nickname", "BlueSUN")
        ns.address "Shanghai"
        ns.friends(num: friendList.size()) {
            for (f in friendList) {
                ns.friend f
            }
        }
    }
}

println xml


//--------------------------------------------------
def rootNode = new XmlParser().parseText(out.toString());
println rootNode.depthFirst().size();
def name1 = rootNode.myName[0];
println name1.name();
println name1.'@type';
println name1.text();

println "----------------------------"
def myNameContent = rootNode.myName.findAll(){ it.'@type'.contains('Name') }.size();
println myNameContent

myNameContent = rootNode.myName.findAll{ it.text() == "���λ�a" }.'@type';
println (myNameContent);

myNameContent = rootNode.depthFirst().grep{ it.'@type' }.'@type';
println (myNameContent);

myNameContent = rootNode.myName.sort{ it.'@type' }.'@type';
println (myNameContent);

println "-----------set value-------------------"
name1.value = "��";
println rootNode.myName[0].text();

